package api

import (
	"flag"
	"github.com/gin-gonic/gin"
	pb "num_srv/proto"
	"week2_api/internal/api/request"
	"week2_api/internal/api/response"
	"week2_api/internal/handler"
)

var (
	numServer = flag.String("NumServer", "127.0.0.1:50052", "The server address")
	numSrv    = handler.NewClient().NumServer(numServer)
)

func Create(c *gin.Context) {
	var json *request.Num
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}
	create, err := numSrv.Create(c, &pb.CreateRequest{
		IolUserId: int64(json.IolUserId),
		DoctorId:  int64(json.DoctorId),
		Type:      int64(json.Type),
		Why:       json.Why,
		Methods:   json.Methods,
	})
	if err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}
	response.Response(200, "预约成功", create, c)
}

func List(c *gin.Context) {
	var json *request.NumList
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}

	list, err := numSrv.List(c, &pb.ListRequest{NumId: int64(json.NumId)})

	if err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}
	response.Response(200, "展示成功", list, c)

}

func Update(c *gin.Context) {
	var json *request.Update
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}

	update, err := numSrv.Update(c, &pb.UpdateRequest{
		IolUserId: int64(json.IolUserId),
		DoctorId:  int64(json.DoctorId),
		Type:      int64(json.Type),
		Why:       json.Why,
		Methods:   json.Methods,
		NumId:     int64(json.NumId),
	})

	if err != nil {
		response.Response(400, err.Error(), nil, c)
		return
	}
	response.Response(200, "修改成功", update, c)

}
